package com.example.knowledgeget1.KnowtController;

import com.example.knowledgeget1.ChatCommon.Entity.NodeType;
import com.example.knowledgeget1.ChatCommon.Service.NodeTypeService;
import com.example.knowledgeget1.Common.HttpStatusEnum;
import com.example.knowledgeget1.Common.ResponseResult;
import com.example.knowledgeget1.KnowtCommon.Entity.Course;
import com.example.knowledgeget1.KnowtCommon.Response.CoursesResponse;
import com.example.knowledgeget1.KnowtCommon.Service.CourseService;
import com.example.knowledgeget1.KnowtCommon.Service.KnowinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private NodeTypeService nodeTypeService;
    @Autowired
    private KnowinService knowinService;

    // 创建新课程
    @PostMapping("/")
    public Course createCourse(@RequestBody Course course) {
        return courseService.createCourse(course);
    }

    // 获取所有课程
    @GetMapping("/")
    public List<Course> getAllCourses() {
        return courseService.getAllCourses();
    }

    // 通过ID获取单个课程
    @GetMapping("/{id}")
    public Course getCourseById(@PathVariable Long id) {
        return courseService.getCourseById(id);
    }

    // 更新课程信息
    @PutMapping("/{id}")
    public Course updateCourse(@PathVariable Long id, @RequestBody Course course) {
        return courseService.updateCourse(id, course);
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public void deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
    }

    // 根据学生ID获取课程列表
    @GetMapping("/student")
    public ResponseResult getCoursesByStudentId(@RequestParam(value = "stuId") Long stuId) {
        List<CoursesResponse> coursesResponses = courseService.getCoursesByStudentId(stuId);
        if (coursesResponses.isEmpty()) {
            return ResponseResult.success("该学生没有课程");
        }
        return ResponseResult.success(coursesResponses);
    }

    // 根据教师id获取课程列表
    @GetMapping("/teacher")
    public ResponseResult getCoursesByTeacherId(@RequestParam(value = "teachId") Long teachId) {
        List<CoursesResponse> coursesResponses = courseService.getCoursesByTeacherId(teachId);
        if(coursesResponses.isEmpty()){
            return ResponseResult.success("该教师没有课程");
        }
        return ResponseResult.success(coursesResponses);
    }

    // 学生获取课程下的知识点
    @GetMapping("/knowinstu/{courseId}")
    public ResponseResult getStuNodeTypesByCourseId(@PathVariable Long courseId) {
        List<Long> knowIds = knowinService.getKnowIdsByCourseId(courseId);
        List<NodeType> nodeTypes = nodeTypeService.getNodeTypesByKnowIdsRelease(knowIds);
        if (nodeTypes.isEmpty()) {
            return ResponseResult.fail(HttpStatusEnum.NOT_FOUND);
        }
        return ResponseResult.success(nodeTypes);
    }

    // 教师获取课程下的知识点
    @GetMapping("/knowintea/{courseId}")
    public ResponseResult getTeaNodeTypesByCourseId(@PathVariable Long courseId) {
        List<Long> knowIds = knowinService.getKnowIdsByCourseId(courseId);
        List<NodeType> nodeTypes = nodeTypeService.getNodeTypesByKnowIds(knowIds);
        if (nodeTypes.isEmpty()) {
            return ResponseResult.fail(HttpStatusEnum.NOT_FOUND);
        }
        return ResponseResult.success(nodeTypes);
    }
}
